﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace COMPONENT.Library.General
{
    public static class clsSeguridad
    {
        public static string Encriptar(string cadena)
        {
            string resp = string.Empty;
            try
            {
                TripleDESCryptoServiceProvider objToEncrip = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteHash, byteBuffer;
                string key = "richardesmanco";
                byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                objHashMD5 = null;
                objToEncrip.Key = byteHash;
                objToEncrip.Mode = CipherMode.ECB;
                byteBuffer = ASCIIEncoding.ASCII.GetBytes(cadena);
                resp =  Convert.ToBase64String(objToEncrip.CreateEncryptor().TransformFinalBlock(byteBuffer, 0, byteBuffer.Length));
            }
            catch (Exception ex)
            {
            }
            return resp;
        }

        public static string Desencriptar(string cadena)
        {
            string resp = string.Empty;
            try
            {
                TripleDESCryptoServiceProvider objDesc = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteHash, byteBuffer;
                string key = "richardesmanco";
                byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                objHashMD5 = null;
                objDesc.Key = byteHash;
                objDesc.Mode = CipherMode.ECB;
                byteBuffer = Convert.FromBase64String(cadena);
                resp = ASCIIEncoding.ASCII.GetString(objDesc.CreateDecryptor().TransformFinalBlock(byteBuffer, 0, byteBuffer.Length));
                objDesc = null;
            }
            catch (Exception ex)
            {

            }
            return resp;
        }
    }
}
